package com.autonavi.yunda.yunji.job.executor.config;

import com.autonavi.yunda.yunji.core.engine.script.FunctionScriptClassManager;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import com.xxl.job.core.handler.TaskJobHandlerManager;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author cizhong.wcs
 * @date 2024/1/17 13:44
 */
public class EngineJobSpringExecutor extends XxlJobSpringExecutor {
    @Resource
    private TaskJobHandlerManager taskJobHandlerManager;
    @Resource
    private FunctionScriptClassManager functionScriptClassManager;

    private static final AtomicBoolean INIT_REFRESH_IS_RUNNING = new AtomicBoolean(false);

    @Override
    public void initAndRefreshEngineJobHandler() {
        if (INIT_REFRESH_IS_RUNNING.compareAndSet(false, true)) {
            try {
                initJobHandlerAndFunctionScript();
            } finally {
                INIT_REFRESH_IS_RUNNING.set(false);
            }
        }
    }

    public void initJobHandlerAndFunctionScript() {
        functionScriptClassManager.checkAndRefreshFunctionScriptManager();
        taskJobHandlerManager.checkAndRefreshJobHandlerManager();
    }

}
